package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.BuildingUser;
import org.springblade.modules.admin.service.IBuildingUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.modules.admin.entity.CustomTag;
import org.springblade.modules.admin.vo.CustomTagVO;
import org.springblade.modules.admin.wrapper.CustomTagWrapper;
import org.springblade.modules.admin.service.ICustomTagService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 客户标签表 控制器
 *
 * @author ysq
 * @since 2023-08-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/customtag")
@Api(value = "客户标签表", tags = "客户标签表接口")
public class CustomTagController extends BladeController {

	private final ICustomTagService customTagService;

	private final IBuildingUserService buildingUserService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入customTag")
	public R<CustomTagVO> detail(CustomTag customTag) {
		CustomTag detail = customTagService.getOne(Condition.getQueryWrapper(customTag));
		return R.data(CustomTagWrapper.build().entityVO(detail));
	}

	/**
	 * 分页 客户标签表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入customTag")
	public R<IPage<CustomTagVO>> list(CustomTag customTag, Query query) {
		IPage<CustomTag> pages = customTagService.page(Condition.getPage(query), Condition.getQueryWrapper(customTag));
		return R.data(CustomTagWrapper.build().pageVO(pages));
	}

	/**
	 * 新增或修改 客户标签表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入customTag")
	public R submit(@Valid @RequestBody CustomTag customTag) {
		return R.status(customTagService.saveOrUpdate(customTag));
	}

	/**
	 * 新增 客户标签表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增", notes = "传入customTag")
	public R save(@Valid @RequestBody CustomTag customTag) {
		return R.status(customTagService.save(customTag));
	}

	/**
	 * 删除 客户标签表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		// 获取 id 列表， 并判断其中是否有id 被 buildingUser 使用
		List<Long> midIds = Func.toLongList(ids);
		for (Long id : midIds) {
			int count = buildingUserService.count(new LambdaQueryWrapper<BuildingUser>()
				.eq(BuildingUser::getTagId, id));
			if(count > 0){
				throw new ServiceException("标签存在已关联使用单位，无法删除");
			}
		}
		return R.status(customTagService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 自定义分页 客户标签表
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 30)
	@ApiOperation(value = "分页", notes = "传入customTag")
	public R<IPage<CustomTagVO>> page(CustomTagVO customTag, Query query) {
		IPage<CustomTagVO> pages = customTagService.selectCustomTagPage(Condition.getPage(query), customTag);
		return R.data(pages);
	}

	/**
	 * 修改 客户标签表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 40)
	@ApiOperation(value = "修改", notes = "传入customTag")
	public R update(@Valid @RequestBody CustomTag customTag) {
		return R.status(customTagService.updateById(customTag));
	}


}
